package com.glela.controller;

import com.alibaba.fastjson.JSONObject;
import com.glela.cache.redis.RedisUtil;
import com.glela.common.util.DateUtil;
import com.glela.goods.model.vo.goods.LiveSaleResponse;
import com.glela.live.model.LivePlan;
import com.glela.live.service.LivePlanService;
import com.glela.live.service.LiveSaleGoodsService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponsePageModel;
import com.glela.platform.util.ParameterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/liveSale")
public class LiveSaleGoodsController extends BaseController{

    @Autowired
    private LiveSaleGoodsService liveSaleGoodsService;

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping(value = "/getLiveSaleGoods",method = RequestMethod.POST,consumes = "application/json")
    public ResponseEntity<?> getLiveSaleGoods(@RequestBody JSONObject jsonObject){
        int id = jsonObject.getIntValue("id");
        if (ParameterUtils.validParams(id)){
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
        }
        /*List<String> redislist = redisUtil.lrange(RedisKeys.LIVE_SALE_GOODS+liveUserId,0,-1);
        List<LiveSaleResponse> list = new ArrayList<>();
        if (!redislist.isEmpty()){
            for(String p : redislist) {
                GoodsListResponse goodsListResponse = JSON.parseObject(p,GoodsListResponse.class);
                list.add(goodsListResponse);
            }
        }else{
            list = liveSaleGoodsService.getLiveSaleByLiveUid(liveUserId);
            String[] toBeStored = new String[list.size()];
            for (int i = 0; i <list.size() ; i++) {
                try {
                    toBeStored[i]= JsonUtil.toJson(list.get(i));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            redisUtil.rpush(RedisKeys.LIVE_SALE_GOODS+liveUserId,toBeStored);
            redisUtil.expire(RedisKeys.LIVE_SALE_GOODS+liveUserId, 300);
        }*/
        List<LiveSaleResponse> list = liveSaleGoodsService.getLiveSaleByLivePlanId(id);
        return toRespEntity(toResultModelJSON(new ResponsePageModel<>(list.size(), 10, 1, list)));
    }

}
